package com.filldream.fastboot.common.okutil.stock.ths.comment;

import cn.hutool.core.util.RandomUtil;
import com.filldream.fastboot.common.okutil.stock.BaseUtil;
import com.filldream.fastboot.common.okutil.stock.ths.comment.dto.ThsComment;
import com.filldream.fastboot.common.okutil.stock.ths.comment.dto.ThsCommentReply;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ThsCommentReadUtil {

    //已经显示且没有新回复的评论
    public static final List<ThsComment> oldCommentList = new ArrayList<>();

    /**
     * 阅读
     * @param codes
     * @return
     */
    public static boolean reads(String codes){
        boolean isRefresh = false;
        String[] split = codes.split(",");
        for (String item : split) {
            boolean read = read(item);
            try {
                Thread.sleep(1000L * RandomUtil.randomLong(1L,3L));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            isRefresh = read || isRefresh;
        }
        return isRefresh;
    }

    /**
     * 阅读评论
     * @param code
     * @return
     */
    public static Boolean read(String code){
        List<ThsComment> waitReadComments = getWaitReadComments(code);
        for (ThsComment item : waitReadComments) {
            //输出内容
            String printContent = item.getPrintContent();
            if( printContent.length() > 90 ){
                continue;
            }
            System.out.println(BaseUtil.xrContent(printContent));
            List<String> post_pic_url = item.getImgArr();
            for (String itemUrl : post_pic_url) {
                System.out.println(BaseUtil.xrReply("  -图片：" + BaseUtil.xrContent(itemUrl)));
            }
            List<ThsCommentReply> realReplyList = item.getComments();
            for (ThsCommentReply reply : realReplyList) {
                System.out.println("  "+BaseUtil.xrReply(reply.getRealReply()));
            }
        }
        return !waitReadComments.isEmpty();
    }

    /**
     * 获取最新的股评和回复
     * @param code
     * @return
     */
    public static List<ThsComment> getWaitReadComments(String code){
        List<ThsComment> waitReadComments = new ArrayList<>();
        List<ThsComment> comments = ThsCommentUtil.getComments(code);
        for (ThsComment comment : comments) {
            ThsComment oldComment = oldCommentList.stream().filter(t -> t.getId().equals(comment.getId())).findFirst().orElse(null);
            //全新的
            if( oldComment == null ){
                waitReadComments.add( comment );
                oldCommentList.add(comment);
            }else{
                //查询一下没有输出的评论
                List<ThsCommentReply> oldCommentRealReplyList = oldComment.getComments();
                List<ThsCommentReply> realReplyList = comment.getComments();
                List<ThsCommentReply> waitReadReplyList = new ArrayList<>();
                for (ThsCommentReply thsCommentReply : realReplyList) {
                    Long replyId = thsCommentReply.getCid();
                    if( oldCommentRealReplyList.stream().noneMatch(t -> t.getCid().equals(replyId)) ){
                        waitReadReplyList.add( thsCommentReply );
                    }
                }
                oldCommentRealReplyList.addAll( waitReadReplyList );
                oldComment.setComments( oldCommentRealReplyList );
                if( !waitReadReplyList.isEmpty() ){
                    waitReadComments.add( oldComment );
                    oldCommentList.removeIf(t -> t.getId().equals(oldComment.getId()));
                    oldCommentList.add(oldComment);
                }
            }
        }
        return waitReadComments;
    }

}